
<?php
require 'db.php';
if(isset($_POST['masv']))$masv=$_POST['masv'];
if(isset($_POST['hocky']))$hocky=$_POST['hocky'];
if(isset($_POST['nienkhoa']))$nienkhoa=$_POST['nienkhoa'];
if(isset($_POST['del'])){
	if(isset($_POST['mamh']))$mamh=$_POST['mamh'];
	$sql = "DELETE FROM dbo_kehoachhttk WHERE MaSV=? and nienkhoa=? and hocky=? and mamh=?";
	$stmt = $db->prepare ( $sql );
	$stmt->bindParam ( "1", $masv );
	$stmt->bindParam ( "2", $nienkhoa );
	$stmt->bindParam ( "3", $hocky );
	$stmt->bindParam ( "4", $mamh );
	$stmt->execute ();
	$success=$stmt->rowCount();
	if(!$success){
		echo "Có lỗi xảy ra vui lòng thử lại sau ";
		exit;
	}else{
		echo 1;
		exit;
	}
}
if(isset($_POST['btnedit'])){
if(isset($_POST['masv']))$masv=$_POST['masv'];
if(isset($_POST['mamh']))$mamh=$_POST['mamh'];
if(isset($_POST['hocky']))$hocky=$_POST['hocky'];
if(isset($_POST['nienkhoa']))$nienkhoa=$_POST['nienkhoa'];
if(isset($_POST['hockynew']))$hockynew=$_POST['hockynew'];
if(isset($_POST['nienkhoanew']))$nienkhoanew=$_POST['nienkhoanew'];
	$sql = "Update dbo_kehoachhttk set hocky=? , nienkhoa=? where masv=? and hocky=? and nienkhoa=? and mamh=?";
	$stmt = $db->prepare ( $sql );
	$stmt->bindParam ( "1", $hockynew );
	$stmt->bindParam ( "2", $nienkhoanew );
	$stmt->bindParam ( "3", $masv );
	$stmt->bindParam ( "4", $hocky );
	$stmt->bindParam ( "5", $nienkhoa );
	$stmt->bindParam ( "6", $mamh );
	$stmt->execute ();
	$success=$stmt->rowCount();
	if(!$success){
		echo "Có lỗi xảy ra vui lòng thử lại sau ";
		exit;
	}else{
		echo 1;
		exit;
	}
}
$sql="SELECT dbo_monhoc.MaMH, TenMH, SoTC, NienKhoa, HocKy FROM dbo_kehoachhttk, dbo_sinhvien, dbo_monhoc ";
$sql.=" WHERE dbo_kehoachhttk.MaMH=dbo_monhoc.MaMH and dbo_sinhvien.MaSV=dbo_kehoachhttk.MaSV and dbo_sinhvien.MaSV=:masv and NienKhoa=:nk and HocKy=:hk ";
$stmt = $db->prepare($sql);
$stmt->bindParam(":hk",$hocky,PDO::PARAM_STR);
$stmt->bindParam(":nk",$nienkhoa,PDO::PARAM_STR);
$stmt->bindParam(":masv",$masv,PDO::PARAM_STR);
$stmt->execute();
$sql3="SELECT dbo_sinhvien.MaSV,Holot, Ten, MaLop FROM dbo_sinhvien WHERE dbo_sinhvien.MaSV=:masv";
$stmt2 = $db->prepare($sql3);
$stmt2->bindParam(":masv",$masv,PDO::PARAM_STR);
$stmt2->execute();
$r3=$stmt2->fetch();

?>
<hr ></hr>

<table  class="table table-bordered table-hover" style="font-size: 13px;">
	
			<thead>
				<div class="row">
				<div class="col-md-12 text-center">
				<p class="form-group alert-info"><label>MSSV :</label><span><?php echo $r3['MaSV'];?></span> <label><?php echo "  -  "?>Họ và tên :</label><span><?php echo $r3['Holot']." ".$r3['Ten']."  -  ";?></span>
				<label>Mã lớp :</label><span><?php echo $r3['MaLop'];?></span>
			</p> </div></div>
			<tr class="info"><th>STT</th><th>Mã học phần</th><th>Tên học phần</th><th>Số tín chỉ</th><th>Năm học</th><th>Học kỳ</th><th>Xóa</th></tr></thead>
			<tbody>
	<?php $i=1;
	
		if($stmt->execute()){
			
				while($r=$stmt->fetch()){
?>
				
				<tr>
					<td><?php echo $i++;?></td>
					<td><?php echo $r['MaMH'];?></td>
					<td><?php echo $r['TenMH'];?></td>
					<td><?php echo $r['SoTC'];?></td>
					<td><select name="ChonNK"  id="ChonNKedit" class="form-control" data-mamh="<?php echo $r['MaMH'];?>">
			
	<?php
	$sql = "SELECT distinct NienKhoa FROM dbo_nienkhoa ORDER BY NienKhoa";
	$result = $db->query ( $sql );
	if ($result) {
		while ( $row = $result->fetch () ) {
			echo "<option value='" . $row ["NienKhoa"] . "'";
			if ($row ["NienKhoa"] == $nienkhoa) {
				echo " selected ";
			}
			echo ">";
			echo $row ["NienKhoa"] . "</option>";
		}
	}
	?>
		</select></td>
					<td><select name="ChonHK" id="ChonHKedit" class=" form-control " data-mamh="<?php echo $r['MaMH'];?>">
		
		<?php
	$sql = "SELECT distinct HocKy FROM dbo_nienkhoa ORDER BY HocKy";
	$result = $db->query ( $sql );
	if ($result) {
		while ($row = $result->fetch()){
			echo "<option value='" . $row ["HocKy"] . "'";
			if ($row ["HocKy"] == $hocky) {
				echo " selected ";
			}
			echo ">";
			if($row ["HocKy"] == "3"){
				echo "Hè";
			}else{
			echo $row ["HocKy"] . "</option>";
		}}
	}
	?>
		</select></td>
				<td><a class="del" id="btnxoakhht" href='#' data-mamon='<?php echo $r['MaMH'];?>' data-tenmon="<?php echo $r['TenMH'];?>"><span class="glyphicon glyphicon-trash"></span></a></td>
		<?php }} ?>	
			</tbody>
			</table>